<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文体会堂预约信息管理系统</title>
    <!-- 引入vue element 使用所需jar包 -->
    <link rel="stylesheet" href="../plugins/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../css/indexBackgroundContain.css">
    <script src="../js/axios-0.18.0.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/element-ui/lib/index.js"></script>
    <link rel="icon" href="../img/favicon.ico" type="image/x-icon"> <!-- 确保 favicon 格式正确 -->
</head>

<body>
<div id="app">
    <div class="header">
        <img src="../img/logo1.png" alt="学校logo" class="logo"> <!-- 左侧logo图片 -->
        <span class="headerPlaceholder"></span>
        <h2 class="textH2">文体会堂预约信息管理系统界面</h2>
    </div>
    <div class="container">
        <div class="sidebar">
            <div class="sidebar-item" :class="{ 'active-module': currentPage === 'loginIndex' }"
                 @click="selectPage('loginIndex')">主界面</div>
            <div class="sidebar-item" :class="{ 'active-module': currentPage === 'noticeManage' }"
                 @click="selectPage('noticeManage')">公告管理</div>
            <div class="sidebar-item" :class="{ 'active-module': currentPage === 'placeManage' }"
                 @click="selectPage('placeManage')">场地管理</div>
            <div class="sidebar-item" :class="{ 'active-module': currentPage === 'placeSubscribe' }"
                 @click="selectPage('placeSubscribe')">预约场地</div>
            <div class="sidebar-item" :class="{ 'active-module': currentPage === 'alreadySubscribe' }"
                 @click="selectPage('alreadySubscribe')">已预约信息</div>
            <div class="sidebar-item" :class="{ 'active-module': currentPage === 'subscribeHistory' }"
                 @click="selectPage('subscribeHistory')">预约历史</div>
            <div class="sidebar-item" :class="{ 'active-module': currentPage === 'usersManage' }"
                 @click="selectPage('usersManage')">用户信息管理</div>
            <div class="sidebar-item" :class="{ 'active-module': currentPage === 'myInfo' }"
                 @click="selectPage('myInfo')">我的信息</div>
        </div>

        <div class="main-content">
            <div>
                <el-form :inline="true" :model="selectInfo" class="demo-form-inline">
                    <el-form-item label="场地类别">
                        <el-select v-model="selectInfo.type" placeholder="请选择场地类别" size="medium">
                            <el-option label="室内乒乓球" value="室内乒乓球场"></el-option>
                            <el-option label="室内羽毛球" value="室内羽毛球场"></el-option>
                            <el-option label="室外乒乓球" value="室外乒乓球场"></el-option>
                            <el-option label="室外篮球" value="室外篮球场"></el-option>
                            <el-option label="全部" value="全部"></el-option>
                        </el-select>
                        <el-button type="primary" plain @click="onSelectSubmit">筛选</el-button>
                    </el-form-item>
                    <span>
                        <el-button type="success" plain @click="dialogVisibleAdd = true">新增场地</el-button>
                    </span>
                </el-form>
            </div>

            <!-- 表格数据 -->
            <template>
                <el-table :data="tableData" height="700" border style="width: 100%">
                    <el-table-column prop="name" label="场地类别" align="center"></el-table-column>
                    <el-table-column prop="site" label="地点" align="center"></el-table-column>
                    <el-table-column prop="facility" label="体育设施" align="center"></el-table-column>
                    <el-table-column prop="description" label="场地描述" align="center"></el-table-column>
                    <el-table-column prop="status" label="场地状态" align="center">
                    </el-table-column>
                    <el-table-column label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </template>



            <!-- 弹出编辑对话框 -->
            <el-dialog
                    title="场地信息编辑框"
                    :visible.sync="dialogVisibleEdit"
                    width="30%">

                <!-- 编辑框表单 -->
                <el-form ref="form" :model="place" label-width="80px">
                    <el-form-item label="场地类别">
                        <el-input v-model="place.name"></el-input>
                    </el-form-item>
                    <el-form-item label="地点">
                        <el-input v-model="place.site"></el-input>
                    </el-form-item>
                    <el-form-item label="体育设施">
                        <el-input v-model="place.facility"></el-input>
                    </el-form-item>
                    <el-form-item label="是否可用">
                        <el-switch v-model="place.status" active-value="可用" inactive-value="维护中"></el-switch>
                    </el-form-item>
                    <el-form-item label="场地描述">
                        <el-input type="textarea" v-model="place.description"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onEditSubmit">保存</el-button>
                        <el-button @click="dialogVisibleEdit = false">取消</el-button>
                    </el-form-item>
                </el-form>
            </el-dialog>

            <!-- 弹出新增对话框 -->
            <el-dialog
                    title="新增场地"
                    :visible.sync="dialogVisibleAdd"
                    width="30%">
                <el-form ref="form" :model="place" label-width="80px">
                    <el-form-item label="场地类别">
                        <el-input v-model="place.name"></el-input>
                    </el-form-item>
                    <el-form-item label="地点">
                        <el-input v-model="place.site"></el-input>
                    </el-form-item>
                    <el-form-item label="体育设施">
                        <el-input v-model="place.facility"></el-input>
                    </el-form-item>
                    <el-form-item label="是否可用">
                        <el-switch v-model="place.status" active-value="可用" inactive-value="维护中"></el-switch>
                    </el-form-item>
                    <el-form-item label="场地描述">
                        <el-input type="textarea" v-model="place.description"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="addHandle">提交</el-button>
                        <el-button  @click="dialogVisibleAdd = false">取消</el-button>
                    </el-form-item>
                </el-form>
            </el-dialog>



        </div>
    </div>
</div>

<script>
    new Vue({
        el: '#app',
        data() {
            return {
                currentPage: '',

                //查询出来的表格数据，用于切换到当前页面时显示到主容器中
                tableData:[],

                //编辑框中的数据
                place:{
                    palceId:-1,
                    name:"",
                    site:"",
                    facility:"",
                    description:"",
                    status:""
                },

                //搜索框的默认条件
                selectInfo:{
                    type:""
                },

                // 添加数据对话框是否展示的标记
                dialogVisibleAdd: false,

                //编辑表单是否可见
                dialogVisibleEdit:false
            };
        },
        methods: {

            selectPage(page) {
                this.currentPage = page; // 设置当前页面标记
                localStorage.setItem('currentPage', page); // 保存当前模块到 localStorage
                const nextUrl = "/pages/" + page + ".html";

                window.location.href = nextUrl; // 页面跳转
            },

            //获取数据
            getTableData(){
                const url = "/places";
                axios.get(url).then((resp)=>{
                    if(resp.data.code == 20001){
                        this.tableData = resp.data.data;
                    }
                })
            },

            //表格中的编辑按钮
            handleEdit(index, row) {
                const url = "/places/byId/"+row.placeId;
                axios.get(url).then((resp)=>{
                    this.place =  resp.data.data;
                    this.dialogVisibleEdit = true;
                })

            },
            onEditSubmit() {
                const url = "/places"
                axios.put(url,this.place).then((resp)=>{
                    if(resp.data.code == 20011){
                        this.editSuccessMsg();
                        this.getTableData();
                    }
                })

                this.dialogVisibleEdit = false;

            },
            //修改成功提示信息
            editSuccessMsg() {
                this.$message({
                    message: '修改成功',
                    type: 'success'
                })
            },


            //表格中的删除按钮
            handleDelete(index, row) {
                const url = "/places/"+ row.placeId;
                this.$confirm('确认删除该场地信息？不建议删除', '提示', {
                    confirmButtonText: '确认',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {

                    axios.delete(url).then((resp)=>{
                        if(resp.data.code == 20021){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.getTableData();
                        }
                    })


                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            //通过状态查询条件信息
            onSelectSubmit(){
                const url = "/places/byType/"+this.selectInfo.type;
                if(this.selectInfo.type != "全部"){
                    axios.get(url).then((resp)=>{
                        if(resp.data.code == 20001){
                            this.tableData = resp.data.data;
                        }
                    })
                }else{
                    this.getTableData();
                }
            },

            //新增数据
            addHandle(){
                this.dialogVisibleAdd = true;
                const url = "/places";
                axios.post(url,this.place).then((resp)=>{
                    if(resp.data.code == 20051){
                        this.addSuccessMsg();
                    }
                }).finally(() => {
                    this.dialogVisibleAdd = false;
                    this.getTableData();
                })
            },

            //修改成功提示信息
            addSuccessMsg() {
                this.$message({
                    message: '新增成功',
                    type: 'success'
                })
            }
        },


        mounted() {
            const savedPage = localStorage.getItem('currentPage');
            if (savedPage) {
                this.currentPage = savedPage; // 恢复选中的模块
            } else {
                this.currentPage = ''; // 默认模块
            }
            this.getTableData(); // 获取表格数据（保持原逻辑）
        }
    });
</script>
</body>
</html>